package co.fitstart.weapp.api.handler.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.weapp.api.handler.SmsSender;
import co.fitstart.weapp.api.rao.SmsRao;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Component("smsSender")
public class SmsSenderImpl implements SmsSender {

    private static final Logger logger = LoggerFactory.getLogger(SmsSender.class);

    @Value("#{config['sms.host']}")
    private String host;

    @Value("#{config['sms.mock']}")
    private boolean mock;

    @Rao
    private SmsRao smsRao;
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.handler.SmsSender#generateVerifyCode(java.lang.String)
     */
    public String generateVerifyCode(String mobile) {
        return RandomStringUtils.randomNumeric(6);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.handler.SmsSender#sendVerifyCode(java.lang.String, java.lang.String)
     */
    public boolean sendVerifyCode(String mobile, String code) {
        HttpResult httpResult = new HttpResult();
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("message", "【人马君】您的验证码是" + code);
        parameters.put("mobiles", mobile);
        parameters.put("time", "0");
        
        logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobiles"));
        
        String url = host + "/send-sms";
        
        logger.info("url is [{}]", url);
        logger.info("pramas is [{}]", JsonUtils.toJson(parameters));
        
        httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSender(url, JsonUtils.toJson(parameters));
        
        logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));
        
        return httpResult.isValid();
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.handler.SmsSender#checkVerifyCode(java.lang.String, java.lang.String, java.lang.String)
     */
    public boolean checkVerifyCode(String mobile, String code, String token) {
        String vtoken = DigestUtils.sha256Hex(StringUtils.connect(mobile, code, "fitstart"));
        return org.apache.commons.lang3.StringUtils.equals(vtoken, token);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.handler.SmsSender#sendMessage(java.lang.String, java.lang.String)
     */
    public boolean sendMessage(String mobile, String content) {
        HttpResult httpResult = new HttpResult();
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("message", content);
        parameters.put("mobiles", mobile);
        parameters.put("time", "0");
        
        logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobiles"));
        
        String url = host + "/send-sms";
        
        logger.info("url is [{}]", url);
        logger.info("pramas is [{}]", JsonUtils.toJson(parameters));
        
        httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSender(url, JsonUtils.toJson(parameters));
        
        logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));
        
        return httpResult.isValid();
    }

}
